<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$return['params']=$params=array('uniacid'=>$_SESSION['uniacid'],);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		if($_RQ['status']==0){
			$params['starttime >']=time();
			$return['params']['starttime >']=time();
			
		}
		
		if($_RQ['status']==1){
			$params['starttime <']=time();
			$params['endtime >']=time();
			
		}
		if($_RQ['status']==2){
			$params['endtime <']=time();
			
		}
		$return['params']['status']=$_RQ['status'];
	}
	//print_r($params);exit();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	//$fields=array('pid','goodsid','merchid','createtime','title','images');
	$list=pdo_getslice('jy_jdpm_special_auction',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['starttime']>time()){
				$item['status']=0;
			}
			if($item['starttime']<time() && time()<$item['endtime']){
				$item['status']=1;
			}
			if(time()>$item['endtime']){
				$item['status']=2;
			}
			$item['starttime']=date('Y/m/d H:i:s',$item['starttime']);
			$item['endtime']=date('Y/m/d H:i:s',$item['endtime']);
			
			$member=get_member($item['merchid']);
			$item['account']=$member['username'];
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		
		'tabs'=>array(
			array('op'=>'edit','title'=>'添加专场'),
		),
		'list'=>array('data'=>'list','key'=>'id','del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'专场ID','type'=>'string'),
			array('name'=>'spicture','title'=>'专场图','type'=>'image'),
			//array('name'=>'good','title'=>'拍品图片','type'=>'image'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'description','title'=>'专场描述','type'=>'string'),
			array('name'=>'special_pledge_type','title'=>'冻结模式','type'=>'radio','options'=>'special_pledge_type'),
			array('name'=>'spledge','title'=>'保证金','type'=>'string'),
			array('name'=>'account','title'=>'所属卖家','type'=>'string'),
			
			//array('name'=>'succtype','title'=>'模式','type'=>'radio','options'=>'succtype'),
			array('name'=>'starttime','title'=>'开始时间','type'=>'string'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status')
			
	//	),
		
		),
		'action'=>array(
			array('op'=>'goods','title'=>'拍品管理','type'=>'href'),
		),
		),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'专场ID','name'=>'id','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'标题','name'=>'title','help'=>''),
			array('type'=>'select','width'=>'15','title'=>'状态','name'=>'status','help'=>'','options'=>'status2'),
		),
	);
	$return['special_pledge_type']=array('0'=>'专场收取','1'=>'分别收取');
	$return['status2']=array(
		array('id'=>1,'title'=>'在拍'),
		array('id'=>0,'title'=>'未开始'),
		array('id'=>2,'title'=>'结束'),
	);
	$return['status']=array('1'=>'在拍','0'=>'未开始','2'=>'结束');
	$return['succtype']=array('0'=>'竞拍','1'=>'成交');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='goods'){
	$sid=intval($_RQ['id']);
	if(!$sid){
		exi('请选择专场！'.$sid,'error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'sid'=>$sid);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('jy_jdpm_auction',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['pname'] = cfc('tools')->substr($item['pname'],0,18);
			if($item['starttime'] > time() && $item['endstatus'] ==0){
				$item['endstatus']=10;
			}
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['starttime']=date('Y/m/d H:i:s',$item['starttime']);
			$item['endtime']=date('Y/m/d H:i:s',$item['endtime']);
			$good=get_goodsinfo($item['goodsid']);
			$item['image']=$good['image'];
			$member=pdo_get('core_members',array('id'=>$good['merchid']),array('avatar','username','nickname'));
			$item['account']=$member['username'];
			$item['avatar'] = $member['avatar']?$member['avatar']:'../addons/jy_jdpm/resource/images/userlogo.jpg';
			$item['nickname']=$member['nickname'];
			$group=	pdo_get('jy_jdpm_goods_group',array('id'=>$good['cid']),array('pid','title'));
			$p=pdo_getcolumn('jy_jdpm_goods_group',array('id'=>$group['pid']),'title');
			$item['group']=$p.'/'.$group['title'];
			$item['id']=$item['id'].'/'.$item['goodsid'];
			$item['recommend']=$item['recommend']?$item['recommend']:0;
			$item['url']=custom_url(array('plugin'=>'jy_jdpm','action'=>'detail','query'=>'pid='.$item['pid']));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'拍卖管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>true,'delall'=>false,'list'=>array(
			array('name'=>'id','title'=>'PID/GID','type'=>'string'),
			array('type'=>'img_string','title'=>'拍品','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'pname','url'=>'url'),
				)
			),
			array('type'=>'img_string','title'=>'卖家','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号：','name'=>'account'),
				array('title'=>'昵称：','name'=>'nickname')
				)
			),
			array('name'=>'group','title'=>'频道/分类','type'=>'string'),
			array('name'=>'succtype','title'=>'模式','type'=>'radio','options'=>'succtype'),
			array('name'=>'starttime','title'=>'开始时间','type'=>'string'),
			array('name'=>'endtime','title'=>'结束时间','type'=>'string'),
			array('name'=>'endstatus','title'=>'状态','type'=>'radio','options'=>'endstatus'),	
		),
		),
	);
	$return['pid']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']));
	$c=array();
	foreach($return['pid'] as $v){
		$n=$v['name'];
		$son=pdo_getall('jy_jdpm_goods_group',array('pid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
		foreach($son as $s){
			$d=$s['id'];
			$c[$n][$d]=$s['name'];
		}
	}
	$return['cid']=$c;
	$return['endstatus']=array('10'=>'待拍','0'=>'在拍','1'=>'成交','2'=>'违约','3'=>'流拍','4'=>'撤拍','-1'=>'待支付');
	$return['succtype']=array('0'=>'竞拍','1'=>'成交');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=$_RQ['id'];
	if($id){
		$return['item']=pdo_get('jy_jdpm_special_auction',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$return['item']['starttime']=date('Y-m-d H:m',$return['item']['starttime']);
		$return['item']['endtime']=date('Y-m-d H:m',$return['item']['endtime']);
		$return['item']['sbanner']=json_decode($return['item']['sbanner'],true);
	}else{
		$return['item']=array();
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'专场列表'),
		),
		'desc'=>'编辑专场',
		'edit'=>array(
				array('type'=>'choose','title'=>'所属用户','name'=>'sellerid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
				array('type'=>'string','title'=>'专场名称','name'=>'title'),
				array('name'=>'description','title'=>'专场描述','type'=>'string'),
				array('name'=>'cid','title'=>'选择商品分类','type'=>'select','options'=>'cid','option_title'=>'title','option_value'=>'id'),
				array('name'=>'starttime','title'=>'开始时间','type'=>'time'),
				array('name'=>'endtime','title'=>'结束时间','type'=>'time'),
				array('name'=>'special_pledge_type','title'=>'保证金收取方式','type'=>'radio','options'=>'special_pledge_type','help'=>'前者仅需缴纳专场保证金，即可享有专场内所有商品的出价。未拍到：专场结束解冻保证金;拍到：支付所有专场订单解冻保证金。后者参拍专场内每件拍品都需要缴纳相应保证金。未拍到：商品结束解冻保证金；拍到：支付订单解冻保证金。'),
				array('name'=>'spledge','title'=>'保证金','type'=>'string','show'=>array('special_pledge_type'=>0)),
				array('name'=>'spicture','title'=>'专场列表图','type'=>'image','help'=>'图片尺寸：宽520px；高220px'),
				array('name'=>'sbanner','title'=>'专场BANNER图','type'=>'images','help'=>'图片尺寸：宽2000px；高300px'),
			)
		);
	$return['special_pledge_type']=array('0'=>'专场收取','1'=>'分别收取');
	$return['cid']=pdo_getall('jy_jdpm_goods_group',array('pid'=>0),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$data=get_postdata();
	
	if(!$data['cid']){
		exi('请选择商品分类','error');
	}
	if(!$data['sellerid']){
		exi('请选择所属用户','error');
	}
	$data['starttime']=strtotime($data['starttime']);
	$data['endtime']=strtotime($data['endtime']);
	$data['sbanner']=ijson_encode($data['sbanner']);
	$id=$data['id'];
	unset($data['id']);
	if($id){
		pdo_update('jy_jdpm_special_auction',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('编辑成功','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_jdpm_special_auction',$data);
		exi('添加成功','','list');
	}
	

}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_jdpm_auction',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
		exi('删除成功');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_jdpm_auction',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}